package com.vrv;

import com.vrv.snmp.SnmpServiceV2Impl;
import org.snmp4j.CommunityTarget;
import org.snmp4j.PDU;
import org.snmp4j.Snmp;
import org.snmp4j.event.ResponseEvent;
import org.snmp4j.mp.SnmpConstants;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.OctetString;
import org.snmp4j.smi.UdpAddress;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.transport.DefaultUdpTransportMapping;

import java.util.List;
import java.util.Map;
import java.util.Vector;

public class SnmpExample {

    public static void test()throws Exception{
        SnmpServiceV2Impl snmpService = new SnmpServiceV2Impl("192.168.96.175", 161, "public");
        boolean canCall = snmpService.getCanCall();
        Object oid = snmpService.oid("1.3.6.1.2.1.1.3.0");
        System.out.println(oid);
        /** Cpu利用率*/
        String[] oids = {"1.3.6.1.2.1.25.3.3.1.2"};
        String[] ooids = {"1.3.6.1.2.1.25.3.3.1.2"};
        String[] names = {"cpu"};
        List<Map<String, Object>> maps = snmpService.oidTables(oids, ooids, names, 32);
        System.out.println(maps);


    }

    public static void main(String[] args) {
        try {
            test();

            Snmp snmp = new Snmp(new DefaultUdpTransportMapping()); //构造一个UDP
            snmp.listen(); //开始监听snmp消息

            CommunityTarget target = new CommunityTarget();
            target.setCommunity(new OctetString("public"));//snmpv2的团体名
            target.setVersion(SnmpConstants.version2c);  //snmp版本
            target.setAddress(new UdpAddress("192.168.96.175/161"));
            target.setTimeout(60000); //时延
            target.setRetries(1); //重传

            PDU pdu = new PDU();  //a SNMP protocol data unit
            //使用GETBULK方式获取数据,无论什么方式取oid,都是取叶子节点，叶子节点的父节点都不会取到
            pdu.setType(PDU.GETBULK);

            //snmp getBulk独有
            pdu.setMaxRepetitions(100); //每个OID通过GETBULK方式获取多少个数据
            /*偏移量，假设有两个oid,0代表两个oid都取3000个叶子oid,1代表第一个取它最近的第一个oid,第二个取3000个oid,
             * 大于1的数代表两个oid都是取他们最近的第一个oid
             */
            pdu.setNonRepeaters(0);

            //添加oid,可以多个
            pdu.add(new VariableBinding(new OID("1.3.6.1.2.1.25.3.3.1.2")));
            ResponseEvent responseEvent = snmp.send(pdu, target);
            PDU response = responseEvent.getResponse();

            if (response == null) {
                System.out.println("TimeOut...");
            } else {
                if (response.getErrorStatus() == PDU.noError) {
                    //读取数据
                    Vector<? extends VariableBinding> vbs = response
                            .getVariableBindings();
                    System.out.println(vbs.size());
                    for (VariableBinding vb : vbs) {

                        System.out.println(vb.getOid().toString()+"==="
                                +"====="+vb.getVariable().toString());
                    }
                } else {
                    System.out
                            .println("Error:" + response.getErrorStatusText());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}


